<html>
<head><meta charset="utf-8"><title>constify trait impls · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html">constify trait impls</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214258335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214258335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214258335">(Oct 22 2020 at 23:08)</a>:</h4>
<p>Is there any way to constify some trait impls but not others? It seems like <code>const fn</code>s are not supported in traits yet, but once they are it would be nice to have some impls be const and some not be</p>



<a name="214258359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214258359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214258359">(Oct 22 2020 at 23:09)</a>:</h4>
<p>I guess this is related to the thing where you can't have safe impls of unsafe traits</p>



<a name="214258390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214258390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214258390">(Oct 22 2020 at 23:09)</a>:</h4>
<p>But it would be really nice to have things like <code>String::from</code> be const, but not require all <code>From</code> impls to be const - for one because of compatibility and also because some impls may not be const-able</p>



<a name="214258507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214258507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214258507">(Oct 22 2020 at 23:11)</a>:</h4>
<p>It looks like there's some information <a href="https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md#alternatives">in the RFC</a>, but are there any plans on how we can allow things like <code>String::from</code> to work in const contexts?</p>



<a name="214258583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214258583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214258583">(Oct 22 2020 at 23:12)</a>:</h4>
<p>E.g. it would be nice to be able to concatenate strings in <code>const</code> blocks (this example is somewhat contrived):</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"hello, world"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"hello"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">", world"</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="cm">/* matched */</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="214258894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214258894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214258894">(Oct 22 2020 at 23:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/213817-t-lang/topic/constify.20trait.20impls/near/214258359">said</a>:</p>
<blockquote>
<p>I guess this is related to the thing where you can't have safe impls of unsafe traits</p>
</blockquote>
<p>I realized I never discussed that in the internals thread on this topic <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span>,  but the reason that doesn't work  isn't because of technical stuff but because the word unsafe has a different meaning in both contexts :/</p>



<a name="214259992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214259992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214259992">(Oct 22 2020 at 23:33)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> the general idea of "impls should be able to be more general than the trait" is discussed somewhat in <a href="https://github.com/rust-lang/rfcs/pull/2316">https://github.com/rust-lang/rfcs/pull/2316</a></p>



<a name="214260064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214260064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214260064">(Oct 22 2020 at 23:34)</a>:</h4>
<p>But I think for <code>const</code> the plan is to have something like <code>impl const Default for Foo</code> to make the impl usable in const context.</p>



<a name="214260647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214260647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214260647">(Oct 22 2020 at 23:42)</a>:</h4>
<p>As opposed to <code>const impl</code>? <code>const impl</code> seems like it says more clearly that "it's const only for this impl, not for the trait in general"</p>



<a name="214260783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214260783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214260783">(Oct 22 2020 at 23:44)</a>:</h4>
<p>There's been lots of discussion about that :P  I prefer the other order because <code>unsafe impl</code> is a different kind of thing than <code>const impl</code> would be.  Also because it would be consistent with a bound like <code>where T: const Default</code>.</p>
<p>But it's still very much open what it will actually be.  Could well end up being <code>const impl Default for Foo</code>.</p>



<a name="214260915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214260915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214260915">(Oct 22 2020 at 23:46)</a>:</h4>
<p>Bikeshed for the win! <span aria-label="stuck out tongue wink" class="emoji emoji-1f61c" role="img" title="stuck out tongue wink">:stuck_out_tongue_wink:</span></p>



<a name="214260949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214260949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214260949">(Oct 22 2020 at 23:47)</a>:</h4>
<p>I also try to always thing about <code>const</code> and <code>unsafe</code> together, as they're both "uncarried" effects so usually the same features make sense for both.</p>



<a name="214261062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214261062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214261062">(Oct 22 2020 at 23:49)</a>:</h4>
<p>For example, <code>unsafe impl Foo</code> means "there are unchecked postconditions that the implementation must satisfy", and there's not really an obvious <code>const</code> analogue to that.</p>



<a name="214261119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214261119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214261119">(Oct 22 2020 at 23:50)</a>:</h4>
<p>Over my head <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="214261158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214261158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214261158">(Oct 22 2020 at 23:50)</a>:</h4>
<p>whereas if there's <code>impl const Foo</code> meaning "all the methods are const, even though they're possibly not-const", then I could also imagine <code>impl unsafe Foo</code> meaning "all the methods are <code>unsafe</code>, even though they're usually not", which would be different from <code>unsafe impl Foo</code>.</p>



<a name="214261185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214261185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214261185">(Oct 22 2020 at 23:51)</a>:</h4>
<p>(Not that I'm saying such a feature is necessarily a good thing, but that's what I'd expect it to do if it did exist.)</p>



<a name="214294991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/constify%20trait%20impls/near/214294991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/constify.20trait.20impls.html#214294991">(Oct 23 2020 at 10:10)</a>:</h4>
<p>Relevant tracking issues for the current status of the impl around all this are <a href="https://github.com/rust-lang/rust/issues/67794">https://github.com/rust-lang/rust/issues/67794</a> and <a href="https://github.com/rust-lang/rust/issues/67792">https://github.com/rust-lang/rust/issues/67792</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>